﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports rssctrl
Imports rssparserN

Public Class rssreader
    Public Const faildisplaylen As Int32 = 64

    Protected parsers As rssparserpool = Nothing

    Public Sub New()
        parsers = New rssparserpool()
    End Sub

    Public Function parse(ByRef content As String, ByRef items As deque(Of String) _
                          , Optional ByRef url As String = Nothing) As Boolean
        If items Is Nothing Then
            items = New deque(Of String)
        Else
            items.clear()
        End If
        Dim p As pair(Of rssparser, StreamReader) = Nothing
        Dim index As Int64 = 0
        If parsers.getfree(index, p) Then
            Dim parser As rssparser = Nothing
            Dim reader As StreamReader = Nothing
            parser = p.first
            reader = p.second
            If parser.parse(content) Then
                Dim it As deque(Of pointer(Of xmlcontrol)).iterator = Nothing
                it = parser.channels.begin()
                While it <> parser.channels.end()
                    Dim channel As xmlcontrol = Nothing
                    channel = ++it
                    Dim channeltitle As String = Nothing
                    Dim i As deque(Of xmlcontrol).iterator = Nothing
                    i = channel.children.begin()
                    While i <> channel.children.end()
                        If strsame("title", (+i).tag, False) Then
                            channeltitle = (+i).text()
                            Exit While
                        End If
                        i += 1
                    End While

                    Dim s As source = Nothing
                    If Not isemptystring(channeltitle) Then
                        s = New source(parser.output)
                        s.text = channeltitle
                        s.url = url
                        htmlUtils.escape(s.url)
                    End If
                    i = channel.children.begin()
                    While i <> channel.children.end()
                        If strsame("item", (+i).tag, False) Then
                            With (+i)
                                If Not s Is Nothing Then
                                    .addchild(s)
                                End If
                                If Not .render(Not isdebugmode()) Then
                                    raiseError("cannot render item element, content " + strleft(content, faildisplaylen) _
                                               , errorHandle.errorType.exclamation)
                                Else
                                    parser.output.SetLength(parser.output.Position)
                                    parser.output.Position = 0
                                    items.push_back(reader.ReadToEnd())
                                    parser.output.Position = 0
                                End If
                            End With
                        End If
                        i += 1
                    End While
                    it += 1
                End While
            Else
                raiseError("cannot run rssparser, content " + strleft(content, faildisplaylen) _
                           , errorHandle.errorType.exclamation)
                Return False
            End If
            parser.clear()
            parsers.release(index)
            Return True
        Else
            raiseError("cannot get free rssparser, ignore request.", errorHandle.errorType.exclamation)
            Return False
        End If
    End Function

    Public Shared Sub unittest()
        Console.WriteLine("input a file to parse")
        Dim content As String
        content = System.IO.File.ReadAllText(Console.ReadLine())
        Dim reader As rssreader = Nothing
        reader = New rssreader()
        Dim items As deque(Of String) = Nothing
        reader.parse(content, items, Nothing)
        Dim i As deque(Of String).iterator = Nothing
        i = items.begin()
        While i <> items.end()
            Console.WriteLine(+i)
            Console.WriteLine("**********")
            i += 1
        End While
        pause()
    End Sub
End Class
